package com.kaishengit.core.dao;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;


public class BaseDao<T,PK extends Serializable> {

	private SessionFactory sessionFactory;
	private Class<?> clazz;
	
	public BaseDao(){
		ParameterizedType t = (ParameterizedType) this.getClass().getGenericSuperclass();
		Type[] types = t.getActualTypeArguments();
		clazz = (Class<?>) types[0];
	}

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	public Session getSession(){
		return sessionFactory.getCurrentSession();
	}
	
	public void save(T t){
		getSession().saveOrUpdate(t);
	}
	public void del(T t){
		getSession().delete(t);
	}
	public void del(PK p){
		del(findById(p));
	}
	@SuppressWarnings("unchecked")
	public T findById(PK p){
		return (T) getSession().get(clazz, p);
	}
	@SuppressWarnings("unchecked")
	public List<T> findAll(){
		Criteria cri = getSession().createCriteria(clazz);
		return cri.list();
	}
}
